package constant.milk.periodapp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import constant.milk.periodapp.alarm.MilkAlarm;
import constant.milk.periodapp.bean.SengGaBeData;
import constant.milk.periodapp.bean.SengriData;
import constant.milk.periodapp.database.DBSelectQuery;
import constant.milk.periodapp.database.DbAdapter;
import constant.milk.periodapp.database.DbDeleteQuery;
import constant.milk.periodapp.database.DbInsertQuery;
import constant.milk.periodapp.database.DbUpdateQuery;
import constant.milk.periodapp.dialog.MilkCheckDialog;
import constant.milk.periodapp.dialog.MilkCountDialog;
import constant.milk.periodapp.dialog.MilkCountDialog.CountCallBack;
import constant.milk.periodapp.dialog.MilkMonthDialog;
import constant.milk.periodapp.dialog.MilkMonthDialog.DateCallBank;
import constant.milk.periodapp.dialog.MilkProgressDialog;
import constant.milk.periodapp.dialog.MilkYesNoDialog;
import constant.milk.periodapp.info.InfoActivity;
import constant.milk.periodapp.memo.MemoListActivity;
import constant.milk.periodapp.setting.SengriActivity;
import constant.milk.periodapp.setting.SettingActivity;
import constant.milk.periodapp.tip.TipActivity;
import constant.milk.periodapp.util.MilkShared;
import constant.milk.periodapp.util.MilkUtil;

public class MainActivity extends SuperActivity implements OnClickListener, OnTouchListener
{
	private static final int REQUEST_CODE = 0;
	private static final int REQUEST_SETTING = 1;
	
	private TextView pre, next;
	private TextView todayTextView;
	
	private TextView sunday, monday, tuesday, wednesday, thursday, friday, saturday;
	
	private View calendar1_0;
	private View calendar1_1;
	private View calendar1_2;
	private View calendar1_3;
	private View calendar1_4;
	private View calendar1_5;
	private View calendar1_6;
	
	private View calendar2_0;
	private View calendar2_1;
	private View calendar2_2;
	private View calendar2_3;
	private View calendar2_4;
	private View calendar2_5;
	private View calendar2_6;
	
	private View calendar3_0;
	private View calendar3_1;
	private View calendar3_2;
	private View calendar3_3;
	private View calendar3_4;
	private View calendar3_5;
	private View calendar3_6;

	private View calendar4_0;
	private View calendar4_1;
	private View calendar4_2;
	private View calendar4_3;
	private View calendar4_4;
	private View calendar4_5;
	private View calendar4_6;
	
	private View calendar5_0;
	private View calendar5_1;
	private View calendar5_2;
	private View calendar5_3;
	private View calendar5_4;
	private View calendar5_5;
	private View calendar5_6;
	
	private View calendar6_0;
	private View calendar6_1;
	private View calendar6_2;
	private View calendar6_3;
	private View calendar6_4;
	private View calendar6_5;
	private View calendar6_6;
	
	private TextView calendar1_0TextView;
	private TextView calendar1_1TextView;
	private TextView calendar1_2TextView;
	private TextView calendar1_3TextView;
	private TextView calendar1_4TextView;
	private TextView calendar1_5TextView;
	private TextView calendar1_6TextView;
	
	private TextView calendar2_0TextView;
	private TextView calendar2_1TextView;
	private TextView calendar2_2TextView;
	private TextView calendar2_3TextView;
	private TextView calendar2_4TextView;
	private TextView calendar2_5TextView;
	private TextView calendar2_6TextView;
	
	private TextView calendar3_0TextView;
	private TextView calendar3_1TextView;
	private TextView calendar3_2TextView;
	private TextView calendar3_3TextView;
	private TextView calendar3_4TextView;
	private TextView calendar3_5TextView;
	private TextView calendar3_6TextView;

	private TextView calendar4_0TextView;
	private TextView calendar4_1TextView;
	private TextView calendar4_2TextView;
	private TextView calendar4_3TextView;
	private TextView calendar4_4TextView;
	private TextView calendar4_5TextView;
	private TextView calendar4_6TextView;
	
	private TextView calendar5_0TextView;
	private TextView calendar5_1TextView;
	private TextView calendar5_2TextView;
	private TextView calendar5_3TextView;
	private TextView calendar5_4TextView;
	private TextView calendar5_5TextView;
	private TextView calendar5_6TextView;
	
	private TextView calendar6_0TextView;
	private TextView calendar6_1TextView;
	private TextView calendar6_2TextView;
	private TextView calendar6_3TextView;
	private TextView calendar6_4TextView;
	private TextView calendar6_5TextView;
	private TextView calendar6_6TextView;
	
	private TextView sengri1_0TextView;
	private TextView sengri1_1TextView;
	private TextView sengri1_2TextView;
	private TextView sengri1_3TextView;
	private TextView sengri1_4TextView;
	private TextView sengri1_5TextView;
	private TextView sengri1_6TextView;
	
	private TextView sengri2_0TextView;
	private TextView sengri2_1TextView;
	private TextView sengri2_2TextView;
	private TextView sengri2_3TextView;
	private TextView sengri2_4TextView;
	private TextView sengri2_5TextView;
	private TextView sengri2_6TextView;
	
	private TextView sengri3_0TextView;
	private TextView sengri3_1TextView;
	private TextView sengri3_2TextView;
	private TextView sengri3_3TextView;
	private TextView sengri3_4TextView;
	private TextView sengri3_5TextView;
	private TextView sengri3_6TextView;

	private TextView sengri4_0TextView;
	private TextView sengri4_1TextView;
	private TextView sengri4_2TextView;
	private TextView sengri4_3TextView;
	private TextView sengri4_4TextView;
	private TextView sengri4_5TextView;
	private TextView sengri4_6TextView;
	
	private TextView sengri5_0TextView;
	private TextView sengri5_1TextView;
	private TextView sengri5_2TextView;
	private TextView sengri5_3TextView;
	private TextView sengri5_4TextView;
	private TextView sengri5_5TextView;
	private TextView sengri5_6TextView;
	
	private TextView sengri6_0TextView;
	private TextView sengri6_1TextView;
	private TextView sengri6_2TextView;
	private TextView sengri6_3TextView;
	private TextView sengri6_4TextView;
	private TextView sengri6_5TextView;
	private TextView sengri6_6TextView;
	
	private TextView sex1_0TextView;
	private TextView sex1_1TextView;
	private TextView sex1_2TextView;
	private TextView sex1_3TextView;
	private TextView sex1_4TextView;
	private TextView sex1_5TextView;
	private TextView sex1_6TextView;
	
	private TextView sex2_0TextView;
	private TextView sex2_1TextView;
	private TextView sex2_2TextView;
	private TextView sex2_3TextView;
	private TextView sex2_4TextView;
	private TextView sex2_5TextView;
	private TextView sex2_6TextView;
	
	private TextView sex3_0TextView;
	private TextView sex3_1TextView;
	private TextView sex3_2TextView;
	private TextView sex3_3TextView;
	private TextView sex3_4TextView;
	private TextView sex3_5TextView;
	private TextView sex3_6TextView;

	private TextView sex4_0TextView;
	private TextView sex4_1TextView;
	private TextView sex4_2TextView;
	private TextView sex4_3TextView;
	private TextView sex4_4TextView;
	private TextView sex4_5TextView;
	private TextView sex4_6TextView;
	
	private TextView sex5_0TextView;
	private TextView sex5_1TextView;
	private TextView sex5_2TextView;
	private TextView sex5_3TextView;
	private TextView sex5_4TextView;
	private TextView sex5_5TextView;
	private TextView sex5_6TextView;
	
	private TextView sex6_0TextView;
	private TextView sex6_1TextView;
	private TextView sex6_2TextView;
	private TextView sex6_3TextView;
	private TextView sex6_4TextView;
	private TextView sex6_5TextView;
	private TextView sex6_6TextView;
	
	private TextView memo1_0TextView;
	private TextView memo1_1TextView;
	private TextView memo1_2TextView;
	private TextView memo1_3TextView;
	private TextView memo1_4TextView;
	private TextView memo1_5TextView;
	private TextView memo1_6TextView;
	
	private TextView memo2_0TextView;
	private TextView memo2_1TextView;
	private TextView memo2_2TextView;
	private TextView memo2_3TextView;
	private TextView memo2_4TextView;
	private TextView memo2_5TextView;
	private TextView memo2_6TextView;
	
	private TextView memo3_0TextView;
	private TextView memo3_1TextView;
	private TextView memo3_2TextView;
	private TextView memo3_3TextView;
	private TextView memo3_4TextView;
	private TextView memo3_5TextView;
	private TextView memo3_6TextView;

	private TextView memo4_0TextView;
	private TextView memo4_1TextView;
	private TextView memo4_2TextView;
	private TextView memo4_3TextView;
	private TextView memo4_4TextView;
	private TextView memo4_5TextView;
	private TextView memo4_6TextView;
	
	private TextView memo5_0TextView;
	private TextView memo5_1TextView;
	private TextView memo5_2TextView;
	private TextView memo5_3TextView;
	private TextView memo5_4TextView;
	private TextView memo5_5TextView;
	private TextView memo5_6TextView;
	
	private TextView memo6_0TextView;
	private TextView memo6_1TextView;
	private TextView memo6_2TextView;
	private TextView memo6_3TextView;
	private TextView memo6_4TextView;
	private TextView memo6_5TextView;
	private TextView memo6_6TextView;
	
	private View memoView, statsView, settingView, tipView;
	
	private View weekdayView;
	
	private ArrayList<View> selectList = new ArrayList<View>();
	private ArrayList<TextView> dayList = new ArrayList<TextView>();
	private ArrayList<TextView> sengriList = new ArrayList<TextView>();
	private ArrayList<TextView> sexList = new ArrayList<TextView>();
	private ArrayList<TextView> memoList = new ArrayList<TextView>();
	private ArrayList<SengGaBeData> sgbList = new ArrayList<SengGaBeData>();
	
	private int setYear;
	private int setMonth;
	private int setDay;
	
	private int toYear;
	private int toMonth;
	private int toDay;
	
	private boolean backFlag;
	
	private PopupWindow pw;
	
	private DBSelectQuery select;
	
	private View startView;
	private View endView;
	private View termView;
	private View deleteView;
	
	private MilkYesNoDialog yesNoDialog;
	
	private MilkProgressDialog progressDialog;
	
	private int tharedCount;
	
	private ArrayList<SengriData> sengriCheckList;
	
	private float touchX;
	
	private SengriData selectSengriData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main_activity);
		
		backFlag = false;
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		pre = (TextView) findViewById(R.id.calendar_picker_pre);
		next = (TextView) findViewById(R.id.calendar_picker_next);
		todayTextView = (TextView) findViewById(R.id.calendar_picker_today);
		
		weekdayView = findViewById(R.id.calendarWeekday);
		
		sunday = (TextView) findViewById(R.id.calendarSunday);
		monday = (TextView) findViewById(R.id.calendarMonday); 
		tuesday = (TextView) findViewById(R.id.calendarTuesday); 
		wednesday = (TextView) findViewById(R.id.calendarWednesday); 
		thursday = (TextView) findViewById(R.id.calendarThursday); 
		friday = (TextView) findViewById(R.id.calendarFriday); 
		saturday = (TextView) findViewById(R.id.calendarSaturday);
		
		calendar1_0 = findViewById(R.id.calendar1_0);
		calendar1_1 = findViewById(R.id.calendar1_1);
		calendar1_2 = findViewById(R.id.calendar1_2);
		calendar1_3 = findViewById(R.id.calendar1_3);
		calendar1_4 = findViewById(R.id.calendar1_4);
		calendar1_5 = findViewById(R.id.calendar1_5);
		calendar1_6 = findViewById(R.id.calendar1_6);
		
		calendar2_0 = findViewById(R.id.calendar2_0);
		calendar2_1 = findViewById(R.id.calendar2_1);
		calendar2_2 = findViewById(R.id.calendar2_2);
		calendar2_3 = findViewById(R.id.calendar2_3);
		calendar2_4 = findViewById(R.id.calendar2_4);
		calendar2_5 = findViewById(R.id.calendar2_5);
		calendar2_6 = findViewById(R.id.calendar2_6);
		
		calendar3_0 = findViewById(R.id.calendar3_0);
		calendar3_1 = findViewById(R.id.calendar3_1);
		calendar3_2 = findViewById(R.id.calendar3_2);
		calendar3_3 = findViewById(R.id.calendar3_3);
		calendar3_4 = findViewById(R.id.calendar3_4);
		calendar3_5 = findViewById(R.id.calendar3_5);
		calendar3_6 = findViewById(R.id.calendar3_6);
		
		calendar4_0 = findViewById(R.id.calendar4_0);
		calendar4_1 = findViewById(R.id.calendar4_1);
		calendar4_2 = findViewById(R.id.calendar4_2);
		calendar4_3 = findViewById(R.id.calendar4_3);
		calendar4_4 = findViewById(R.id.calendar4_4);
		calendar4_5 = findViewById(R.id.calendar4_5);
		calendar4_6 = findViewById(R.id.calendar4_6);
		
		calendar5_0 = findViewById(R.id.calendar5_0);
		calendar5_1 = findViewById(R.id.calendar5_1);
		calendar5_2 = findViewById(R.id.calendar5_2);
		calendar5_3 = findViewById(R.id.calendar5_3);
		calendar5_4 = findViewById(R.id.calendar5_4);
		calendar5_5 = findViewById(R.id.calendar5_5);
		calendar5_6 = findViewById(R.id.calendar5_6);
		
		calendar6_0 = findViewById(R.id.calendar6_0);
		calendar6_1 = findViewById(R.id.calendar6_1);
		calendar6_2 = findViewById(R.id.calendar6_2);
		calendar6_3 = findViewById(R.id.calendar6_3);
		calendar6_4 = findViewById(R.id.calendar6_4);
		calendar6_5 = findViewById(R.id.calendar6_5);
		calendar6_6 = findViewById(R.id.calendar6_6);
		
		calendar1_0TextView = (TextView) findViewById(R.id.calendar1_0TextView);
		calendar1_1TextView = (TextView) findViewById(R.id.calendar1_1TextView);
		calendar1_2TextView = (TextView) findViewById(R.id.calendar1_2TextView);
		calendar1_3TextView = (TextView) findViewById(R.id.calendar1_3TextView);
		calendar1_4TextView = (TextView) findViewById(R.id.calendar1_4TextView);
		calendar1_5TextView = (TextView) findViewById(R.id.calendar1_5TextView);
		calendar1_6TextView = (TextView) findViewById(R.id.calendar1_6TextView);
		
		calendar2_0TextView = (TextView) findViewById(R.id.calendar2_0TextView);
		calendar2_1TextView = (TextView) findViewById(R.id.calendar2_1TextView);
		calendar2_2TextView = (TextView) findViewById(R.id.calendar2_2TextView);
		calendar2_3TextView = (TextView) findViewById(R.id.calendar2_3TextView);
		calendar2_4TextView = (TextView) findViewById(R.id.calendar2_4TextView);
		calendar2_5TextView = (TextView) findViewById(R.id.calendar2_5TextView);
		calendar2_6TextView = (TextView) findViewById(R.id.calendar2_6TextView);
		
		calendar3_0TextView = (TextView) findViewById(R.id.calendar3_0TextView);
		calendar3_1TextView = (TextView) findViewById(R.id.calendar3_1TextView);
		calendar3_2TextView = (TextView) findViewById(R.id.calendar3_2TextView);
		calendar3_3TextView = (TextView) findViewById(R.id.calendar3_3TextView);
		calendar3_4TextView = (TextView) findViewById(R.id.calendar3_4TextView);
		calendar3_5TextView = (TextView) findViewById(R.id.calendar3_5TextView);
		calendar3_6TextView = (TextView) findViewById(R.id.calendar3_6TextView);
		
		calendar4_0TextView = (TextView) findViewById(R.id.calendar4_0TextView);
		calendar4_1TextView = (TextView) findViewById(R.id.calendar4_1TextView);
		calendar4_2TextView = (TextView) findViewById(R.id.calendar4_2TextView);
		calendar4_3TextView = (TextView) findViewById(R.id.calendar4_3TextView);
		calendar4_4TextView = (TextView) findViewById(R.id.calendar4_4TextView);
		calendar4_5TextView = (TextView) findViewById(R.id.calendar4_5TextView);
		calendar4_6TextView = (TextView) findViewById(R.id.calendar4_6TextView);
		
		calendar5_0TextView = (TextView) findViewById(R.id.calendar5_0TextView);
		calendar5_1TextView = (TextView) findViewById(R.id.calendar5_1TextView);
		calendar5_2TextView = (TextView) findViewById(R.id.calendar5_2TextView);
		calendar5_3TextView = (TextView) findViewById(R.id.calendar5_3TextView);
		calendar5_4TextView = (TextView) findViewById(R.id.calendar5_4TextView);
		calendar5_5TextView = (TextView) findViewById(R.id.calendar5_5TextView);
		calendar5_6TextView = (TextView) findViewById(R.id.calendar5_6TextView);
		
		calendar6_0TextView = (TextView) findViewById(R.id.calendar6_0TextView);
		calendar6_1TextView = (TextView) findViewById(R.id.calendar6_1TextView);
		calendar6_2TextView = (TextView) findViewById(R.id.calendar6_2TextView);
		calendar6_3TextView = (TextView) findViewById(R.id.calendar6_3TextView);
		calendar6_4TextView = (TextView) findViewById(R.id.calendar6_4TextView);
		calendar6_5TextView = (TextView) findViewById(R.id.calendar6_5TextView);
		calendar6_6TextView = (TextView) findViewById(R.id.calendar6_6TextView);
		
		sengri1_0TextView = (TextView) findViewById(R.id.calendar1_0SengriTextView);
		sengri1_1TextView = (TextView) findViewById(R.id.calendar1_1SengriTextView);
		sengri1_2TextView = (TextView) findViewById(R.id.calendar1_2SengriTextView);
		sengri1_3TextView = (TextView) findViewById(R.id.calendar1_3SengriTextView);
		sengri1_4TextView = (TextView) findViewById(R.id.calendar1_4SengriTextView);
		sengri1_5TextView = (TextView) findViewById(R.id.calendar1_5SengriTextView);
		sengri1_6TextView = (TextView) findViewById(R.id.calendar1_6SengriTextView);
		
		sengri2_0TextView = (TextView) findViewById(R.id.calendar2_0SengriTextView);
		sengri2_1TextView = (TextView) findViewById(R.id.calendar2_1SengriTextView);
		sengri2_2TextView = (TextView) findViewById(R.id.calendar2_2SengriTextView);
		sengri2_3TextView = (TextView) findViewById(R.id.calendar2_3SengriTextView);
		sengri2_4TextView = (TextView) findViewById(R.id.calendar2_4SengriTextView);
		sengri2_5TextView = (TextView) findViewById(R.id.calendar2_5SengriTextView);
		sengri2_6TextView = (TextView) findViewById(R.id.calendar2_6SengriTextView);
		
		sengri3_0TextView = (TextView) findViewById(R.id.calendar3_0SengriTextView);
		sengri3_1TextView = (TextView) findViewById(R.id.calendar3_1SengriTextView);
		sengri3_2TextView = (TextView) findViewById(R.id.calendar3_2SengriTextView);
		sengri3_3TextView = (TextView) findViewById(R.id.calendar3_3SengriTextView);
		sengri3_4TextView = (TextView) findViewById(R.id.calendar3_4SengriTextView);
		sengri3_5TextView = (TextView) findViewById(R.id.calendar3_5SengriTextView);
		sengri3_6TextView = (TextView) findViewById(R.id.calendar3_6SengriTextView);
		
		sengri4_0TextView = (TextView) findViewById(R.id.calendar4_0SengriTextView);
		sengri4_1TextView = (TextView) findViewById(R.id.calendar4_1SengriTextView);
		sengri4_2TextView = (TextView) findViewById(R.id.calendar4_2SengriTextView);
		sengri4_3TextView = (TextView) findViewById(R.id.calendar4_3SengriTextView);
		sengri4_4TextView = (TextView) findViewById(R.id.calendar4_4SengriTextView);
		sengri4_5TextView = (TextView) findViewById(R.id.calendar4_5SengriTextView);
		sengri4_6TextView = (TextView) findViewById(R.id.calendar4_6SengriTextView);
		
		sengri5_0TextView = (TextView) findViewById(R.id.calendar5_0SengriTextView);
		sengri5_1TextView = (TextView) findViewById(R.id.calendar5_1SengriTextView);
		sengri5_2TextView = (TextView) findViewById(R.id.calendar5_2SengriTextView);
		sengri5_3TextView = (TextView) findViewById(R.id.calendar5_3SengriTextView);
		sengri5_4TextView = (TextView) findViewById(R.id.calendar5_4SengriTextView);
		sengri5_5TextView = (TextView) findViewById(R.id.calendar5_5SengriTextView);
		sengri5_6TextView = (TextView) findViewById(R.id.calendar5_6SengriTextView);
		
		sengri6_0TextView = (TextView) findViewById(R.id.calendar6_0SengriTextView);
		sengri6_1TextView = (TextView) findViewById(R.id.calendar6_1SengriTextView);
		sengri6_2TextView = (TextView) findViewById(R.id.calendar6_2SengriTextView);
		sengri6_3TextView = (TextView) findViewById(R.id.calendar6_3SengriTextView);
		sengri6_4TextView = (TextView) findViewById(R.id.calendar6_4SengriTextView);
		sengri6_5TextView = (TextView) findViewById(R.id.calendar6_5SengriTextView);
		sengri6_6TextView = (TextView) findViewById(R.id.calendar6_6SengriTextView);
		
		sex1_0TextView = (TextView) findViewById(R.id.calendar1_0SexTextView);
		sex1_1TextView = (TextView) findViewById(R.id.calendar1_1SexTextView);
		sex1_2TextView = (TextView) findViewById(R.id.calendar1_2SexTextView);
		sex1_3TextView = (TextView) findViewById(R.id.calendar1_3SexTextView);
		sex1_4TextView = (TextView) findViewById(R.id.calendar1_4SexTextView);
		sex1_5TextView = (TextView) findViewById(R.id.calendar1_5SexTextView);
		sex1_6TextView = (TextView) findViewById(R.id.calendar1_6SexTextView);
		
		sex2_0TextView = (TextView) findViewById(R.id.calendar2_0SexTextView);
		sex2_1TextView = (TextView) findViewById(R.id.calendar2_1SexTextView);
		sex2_2TextView = (TextView) findViewById(R.id.calendar2_2SexTextView);
		sex2_3TextView = (TextView) findViewById(R.id.calendar2_3SexTextView);
		sex2_4TextView = (TextView) findViewById(R.id.calendar2_4SexTextView);
		sex2_5TextView = (TextView) findViewById(R.id.calendar2_5SexTextView);
		sex2_6TextView = (TextView) findViewById(R.id.calendar2_6SexTextView);
		
		sex3_0TextView = (TextView) findViewById(R.id.calendar3_0SexTextView);
		sex3_1TextView = (TextView) findViewById(R.id.calendar3_1SexTextView);
		sex3_2TextView = (TextView) findViewById(R.id.calendar3_2SexTextView);
		sex3_3TextView = (TextView) findViewById(R.id.calendar3_3SexTextView);
		sex3_4TextView = (TextView) findViewById(R.id.calendar3_4SexTextView);
		sex3_5TextView = (TextView) findViewById(R.id.calendar3_5SexTextView);
		sex3_6TextView = (TextView) findViewById(R.id.calendar3_6SexTextView);
		
		sex4_0TextView = (TextView) findViewById(R.id.calendar4_0SexTextView);
		sex4_1TextView = (TextView) findViewById(R.id.calendar4_1SexTextView);
		sex4_2TextView = (TextView) findViewById(R.id.calendar4_2SexTextView);
		sex4_3TextView = (TextView) findViewById(R.id.calendar4_3SexTextView);
		sex4_4TextView = (TextView) findViewById(R.id.calendar4_4SexTextView);
		sex4_5TextView = (TextView) findViewById(R.id.calendar4_5SexTextView);
		sex4_6TextView = (TextView) findViewById(R.id.calendar4_6SexTextView);
		
		sex5_0TextView = (TextView) findViewById(R.id.calendar5_0SexTextView);
		sex5_1TextView = (TextView) findViewById(R.id.calendar5_1SexTextView);
		sex5_2TextView = (TextView) findViewById(R.id.calendar5_2SexTextView);
		sex5_3TextView = (TextView) findViewById(R.id.calendar5_3SexTextView);
		sex5_4TextView = (TextView) findViewById(R.id.calendar5_4SexTextView);
		sex5_5TextView = (TextView) findViewById(R.id.calendar5_5SexTextView);
		sex5_6TextView = (TextView) findViewById(R.id.calendar5_6SexTextView);
		
		sex6_0TextView = (TextView) findViewById(R.id.calendar6_0SexTextView);
		sex6_1TextView = (TextView) findViewById(R.id.calendar6_1SexTextView);
		sex6_2TextView = (TextView) findViewById(R.id.calendar6_2SexTextView);
		sex6_3TextView = (TextView) findViewById(R.id.calendar6_3SexTextView);
		sex6_4TextView = (TextView) findViewById(R.id.calendar6_4SexTextView);
		sex6_5TextView = (TextView) findViewById(R.id.calendar6_5SexTextView);
		sex6_6TextView = (TextView) findViewById(R.id.calendar6_6SexTextView);
		
		memo1_0TextView = (TextView) findViewById(R.id.calendar1_0MemoTextView);
		memo1_1TextView = (TextView) findViewById(R.id.calendar1_1MemoTextView);
		memo1_2TextView = (TextView) findViewById(R.id.calendar1_2MemoTextView);
		memo1_3TextView = (TextView) findViewById(R.id.calendar1_3MemoTextView);
		memo1_4TextView = (TextView) findViewById(R.id.calendar1_4MemoTextView);
		memo1_5TextView = (TextView) findViewById(R.id.calendar1_5MemoTextView);
		memo1_6TextView = (TextView) findViewById(R.id.calendar1_6MemoTextView);
		
		memo2_0TextView = (TextView) findViewById(R.id.calendar2_0MemoTextView);
		memo2_1TextView = (TextView) findViewById(R.id.calendar2_1MemoTextView);
		memo2_2TextView = (TextView) findViewById(R.id.calendar2_2MemoTextView);
		memo2_3TextView = (TextView) findViewById(R.id.calendar2_3MemoTextView);
		memo2_4TextView = (TextView) findViewById(R.id.calendar2_4MemoTextView);
		memo2_5TextView = (TextView) findViewById(R.id.calendar2_5MemoTextView);
		memo2_6TextView = (TextView) findViewById(R.id.calendar2_6MemoTextView);
		
		memo3_0TextView = (TextView) findViewById(R.id.calendar3_0MemoTextView);
		memo3_1TextView = (TextView) findViewById(R.id.calendar3_1MemoTextView);
		memo3_2TextView = (TextView) findViewById(R.id.calendar3_2MemoTextView);
		memo3_3TextView = (TextView) findViewById(R.id.calendar3_3MemoTextView);
		memo3_4TextView = (TextView) findViewById(R.id.calendar3_4MemoTextView);
		memo3_5TextView = (TextView) findViewById(R.id.calendar3_5MemoTextView);
		memo3_6TextView = (TextView) findViewById(R.id.calendar3_6MemoTextView);
		
		memo4_0TextView = (TextView) findViewById(R.id.calendar4_0MemoTextView);
		memo4_1TextView = (TextView) findViewById(R.id.calendar4_1MemoTextView);
		memo4_2TextView = (TextView) findViewById(R.id.calendar4_2MemoTextView);
		memo4_3TextView = (TextView) findViewById(R.id.calendar4_3MemoTextView);
		memo4_4TextView = (TextView) findViewById(R.id.calendar4_4MemoTextView);
		memo4_5TextView = (TextView) findViewById(R.id.calendar4_5MemoTextView);
		memo4_6TextView = (TextView) findViewById(R.id.calendar4_6MemoTextView);
		
		memo5_0TextView = (TextView) findViewById(R.id.calendar5_0MemoTextView);
		memo5_1TextView = (TextView) findViewById(R.id.calendar5_1MemoTextView);
		memo5_2TextView = (TextView) findViewById(R.id.calendar5_2MemoTextView);
		memo5_3TextView = (TextView) findViewById(R.id.calendar5_3MemoTextView);
		memo5_4TextView = (TextView) findViewById(R.id.calendar5_4MemoTextView);
		memo5_5TextView = (TextView) findViewById(R.id.calendar5_5MemoTextView);
		memo5_6TextView = (TextView) findViewById(R.id.calendar5_6MemoTextView);
		
		memo6_0TextView = (TextView) findViewById(R.id.calendar6_0MemoTextView);
		memo6_1TextView = (TextView) findViewById(R.id.calendar6_1MemoTextView);
		memo6_2TextView = (TextView) findViewById(R.id.calendar6_2MemoTextView);
		memo6_3TextView = (TextView) findViewById(R.id.calendar6_3MemoTextView);
		memo6_4TextView = (TextView) findViewById(R.id.calendar6_4MemoTextView);
		memo6_5TextView = (TextView) findViewById(R.id.calendar6_5MemoTextView);
		memo6_6TextView = (TextView) findViewById(R.id.calendar6_6MemoTextView);
		
		memoView = findViewById(R.id.mainMemoView);
		statsView = findViewById(R.id.mainInfoView);
		settingView = findViewById(R.id.mainSettingView);
		tipView = findViewById(R.id.mainTipView);
	}

	@Override
	protected void setOnClick()
	{
		pre.setOnTouchListener(MainActivity.this);
		next.setOnTouchListener(MainActivity.this);
		
		todayTextView.setOnTouchListener(MainActivity.this);
		
		memoView.setOnTouchListener(MainActivity.this);
		statsView.setOnTouchListener(MainActivity.this);
		settingView.setOnTouchListener(MainActivity.this);
		tipView.setOnTouchListener(MainActivity.this);
		
		calendar1_0.setOnTouchListener(MainActivity.this);
		calendar1_1.setOnTouchListener(MainActivity.this);
		calendar1_2.setOnTouchListener(MainActivity.this);
		calendar1_3.setOnTouchListener(MainActivity.this);
		calendar1_4.setOnTouchListener(MainActivity.this);
		calendar1_5.setOnTouchListener(MainActivity.this);
		calendar1_6.setOnTouchListener(MainActivity.this);
		
		calendar2_0.setOnTouchListener(MainActivity.this);
		calendar2_1.setOnTouchListener(MainActivity.this);
		calendar2_2.setOnTouchListener(MainActivity.this);
		calendar2_3.setOnTouchListener(MainActivity.this);
		calendar2_4.setOnTouchListener(MainActivity.this);
		calendar2_5.setOnTouchListener(MainActivity.this);
		calendar2_6.setOnTouchListener(MainActivity.this);
		
		calendar3_0.setOnTouchListener(MainActivity.this);
		calendar3_1.setOnTouchListener(MainActivity.this);
		calendar3_2.setOnTouchListener(MainActivity.this);
		calendar3_3.setOnTouchListener(MainActivity.this);
		calendar3_4.setOnTouchListener(MainActivity.this);
		calendar3_5.setOnTouchListener(MainActivity.this);
		calendar3_6.setOnTouchListener(MainActivity.this);
		
		calendar4_0.setOnTouchListener(MainActivity.this);
		calendar4_1.setOnTouchListener(MainActivity.this);
		calendar4_2.setOnTouchListener(MainActivity.this);
		calendar4_3.setOnTouchListener(MainActivity.this);
		calendar4_4.setOnTouchListener(MainActivity.this);
		calendar4_5.setOnTouchListener(MainActivity.this);
		calendar4_6.setOnTouchListener(MainActivity.this);
		
		calendar5_0.setOnTouchListener(MainActivity.this);
		calendar5_1.setOnTouchListener(MainActivity.this);
		calendar5_2.setOnTouchListener(MainActivity.this);
		calendar5_3.setOnTouchListener(MainActivity.this);
		calendar5_4.setOnTouchListener(MainActivity.this);
		calendar5_5.setOnTouchListener(MainActivity.this);
		calendar5_6.setOnTouchListener(MainActivity.this);
		
		calendar6_0.setOnTouchListener(MainActivity.this);
		calendar6_1.setOnTouchListener(MainActivity.this);
		calendar6_2.setOnTouchListener(MainActivity.this);
		calendar6_3.setOnTouchListener(MainActivity.this);
		calendar6_4.setOnTouchListener(MainActivity.this);
		calendar6_5.setOnTouchListener(MainActivity.this);
		calendar6_6.setOnTouchListener(MainActivity.this);
		
		pre.setOnClickListener(MainActivity.this);
		next.setOnClickListener(MainActivity.this);
		
		todayTextView.setOnClickListener(MainActivity.this);
		
		memoView.setOnClickListener(MainActivity.this);
		statsView.setOnClickListener(MainActivity.this);
		settingView.setOnClickListener(MainActivity.this);
		tipView.setOnClickListener(MainActivity.this);
		
		calendar1_0.setOnClickListener(MainActivity.this);
		calendar1_1.setOnClickListener(MainActivity.this);
		calendar1_2.setOnClickListener(MainActivity.this);
		calendar1_3.setOnClickListener(MainActivity.this);
		calendar1_4.setOnClickListener(MainActivity.this);
		calendar1_5.setOnClickListener(MainActivity.this);
		calendar1_6.setOnClickListener(MainActivity.this);
		
		calendar2_0.setOnClickListener(MainActivity.this);
		calendar2_1.setOnClickListener(MainActivity.this);
		calendar2_2.setOnClickListener(MainActivity.this);
		calendar2_3.setOnClickListener(MainActivity.this);
		calendar2_4.setOnClickListener(MainActivity.this);
		calendar2_5.setOnClickListener(MainActivity.this);
		calendar2_6.setOnClickListener(MainActivity.this);
		
		calendar3_0.setOnClickListener(MainActivity.this);
		calendar3_1.setOnClickListener(MainActivity.this);
		calendar3_2.setOnClickListener(MainActivity.this);
		calendar3_3.setOnClickListener(MainActivity.this);
		calendar3_4.setOnClickListener(MainActivity.this);
		calendar3_5.setOnClickListener(MainActivity.this);
		calendar3_6.setOnClickListener(MainActivity.this);
		
		calendar4_0.setOnClickListener(MainActivity.this);
		calendar4_1.setOnClickListener(MainActivity.this);
		calendar4_2.setOnClickListener(MainActivity.this);
		calendar4_3.setOnClickListener(MainActivity.this);
		calendar4_4.setOnClickListener(MainActivity.this);
		calendar4_5.setOnClickListener(MainActivity.this);
		calendar4_6.setOnClickListener(MainActivity.this);
		
		calendar5_0.setOnClickListener(MainActivity.this);
		calendar5_1.setOnClickListener(MainActivity.this);
		calendar5_2.setOnClickListener(MainActivity.this);
		calendar5_3.setOnClickListener(MainActivity.this);
		calendar5_4.setOnClickListener(MainActivity.this);
		calendar5_5.setOnClickListener(MainActivity.this);
		calendar5_6.setOnClickListener(MainActivity.this);
		
		calendar6_0.setOnClickListener(MainActivity.this);
		calendar6_1.setOnClickListener(MainActivity.this);
		calendar6_2.setOnClickListener(MainActivity.this);
		calendar6_3.setOnClickListener(MainActivity.this);
		calendar6_4.setOnClickListener(MainActivity.this);
		calendar6_5.setOnClickListener(MainActivity.this);
		calendar6_6.setOnClickListener(MainActivity.this);
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		setTextList();
		
		sunday.setTextColor(Color.RED);
		saturday.setTextColor(Color.BLUE);
		
		calendar1_0TextView.setTextColor(Color.RED);
		calendar1_6TextView.setTextColor(Color.BLUE);
		
		calendar2_0TextView.setTextColor(Color.RED);
		calendar2_6TextView.setTextColor(Color.BLUE);
		
		calendar3_0TextView.setTextColor(Color.RED);
		calendar3_6TextView.setTextColor(Color.BLUE);
		
		calendar4_0TextView.setTextColor(Color.RED);
		calendar4_6TextView.setTextColor(Color.BLUE);
		
		calendar5_0TextView.setTextColor(Color.RED);
		calendar5_6TextView.setTextColor(Color.BLUE);
		
		calendar6_0TextView.setTextColor(Color.RED);
		calendar6_6TextView.setTextColor(Color.BLUE);
		
		Calendar cal = Calendar.getInstance();
		
		toYear = cal.get(Calendar.YEAR);
		toMonth = cal.get(Calendar.MONTH);
		toDay = cal.get(Calendar.DAY_OF_MONTH);
		
		setYear = cal.get(Calendar.YEAR);
		setMonth = cal.get(Calendar.MONTH);
		setDay = cal.get(Calendar.DAY_OF_MONTH);
		
		for (int i = 0; i < 32; i++)
		{
			SengGaBeData data = new SengGaBeData();
			sgbList.add(data);
		}
		
		setPopWindow();
		
		setCalendar();
	}
	
	private void setPopWindow()
	{
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.popup_window_period, null);
		
		pw = new PopupWindow(view);
		pw.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		pw.setOutsideTouchable(true);
		pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_white));
		
		startView = view.findViewById(R.id.popupStartView);
		endView = view.findViewById(R.id.popupEndView);
		termView = view.findViewById(R.id.popupTermView);
		deleteView = view.findViewById(R.id.popupDeleteView);
		View sexDay = view.findViewById(R.id.popupSexDayView);
		View memo = view.findViewById(R.id.popupMemoView);
		
		startView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkShared s = new MilkShared(superContext);
				int term = s.getSengriTerm();
				
				int date = term / 2;
				
				Calendar cal = Calendar.getInstance();
				cal.set(setYear, setMonth, setDay);
				cal.add(Calendar.DAY_OF_MONTH, -date);
				
				ArrayList<SengriData> list = new ArrayList<SengriData>();
				
				for (int i = 0; i < term; i++)
				{
					cal.add(Calendar.DAY_OF_MONTH, 1);
					list = select.getSengriDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
					
					if (list.size() > 0)
					{
						break;
					}
				}
				
				if (list.size() > 0)
				{
					if (setPeriodUpdate(setYear, setMonth, setDay, list.get(0).getId(), -1))
					{
						setCalendar();
						
						MilkAlarm ma = new MilkAlarm();
						ma.checkAlarm(superContext, superDb);
						
						MilkUtil.showToast(superContext, "생리일 변경에 성공하였습니다.");
					}
					else
					{
						MilkUtil.showToast(superContext, "변경 실패! 개발자에게 문의해주세요.");
					}
				}
				else
				{
					if (setPeriodData(setYear, setMonth, setDay))
					{
						setCalendar();
						
						MilkAlarm ma = new MilkAlarm();
						ma.checkAlarm(superContext, superDb);
						MilkUtil.showToast(superContext, "생리일 입력에 성공하였습니다.");
					}
					else
					{
						MilkUtil.showToast(superContext, "입력 실패! 개발자에게 문의해주세요.");
					}
				}
				
				pw.dismiss();
			}
		});
		
		endView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pw.dismiss();
				
				if (selectSengriData != null)
				{
					int year1 = selectSengriData.getYear();
					int month1 = selectSengriData.getMonth();
					int day1 = selectSengriData.getDay();
					
					for (int i = 0; i < 10; i++)
					{
						Calendar cal = Calendar.getInstance();
						cal.set(setYear, setMonth, setDay);
						cal.add(Calendar.DAY_OF_MONTH, -i);
						int year2 = cal.get(Calendar.YEAR);
						int month2 = cal.get(Calendar.MONTH);
						int day2 = cal.get(Calendar.DAY_OF_MONTH);
						
						if (year1 == year2 && month1 == month2 && day1 == day2)
						{
							if (setPeriodUpdate(year1, month1, day1, selectSengriData.getId(), i+1))
							{
								setCalendar();
								
								MilkUtil.showToast(superContext, "생리기간 수정에 성공하였습니다.");
								return;
							}
							else
							{
								MilkUtil.showToast(superContext, "수정 실패! 개발자에게 문의해주세요.");
								return;
							}
						}
					}
					
					MilkUtil.showToast(superContext, "10일 이전안에 생리일이 없습니다. 개발자에게 문의해주세요.");
				}
				else
				{
					MilkUtil.showToast(superContext, "10일 이전안에 생리일이 없습니다. 개발자에게 문의해주세요.");
					return;
				}
			}
		});
		
		termView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pw.dismiss();
				
				ArrayList<SengriData> list = select.getSengriDate(setYear, setMonth, setDay);
				if (list.size() > 0)
				{
					final SengriData data = list.get(0);
					
					MilkCountDialog dialog = new MilkCountDialog(superContext, "생리기간",
							new CountCallBack()
					{
						@Override
						public void getCount(int count)
						{
							if (setPeriodUpdate(setYear, setMonth, setDay, data.getId(), count))
							{
								setCalendar();
								
								MilkUtil.showToast(superContext, "생리기간 수정에 성공하였습니다.");
							}
							else
							{
								MilkUtil.showToast(superContext, "수정 실패! 개발자에게 문의해주세요.");
							}
						}
					}, data.getTerm());
					
					dialog.show();
				}
				else
				{
					MilkUtil.showToast(superContext, "수정 할 데이터가 없습니다.");
				}
			}
		});
		
		deleteView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pw.dismiss();
				
				yesNoDialog = new MilkYesNoDialog(superContext, "알림", "생리일을 삭제 하시겠습니까?", new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						ArrayList<SengriData> list = select.getSengriDate(setYear, setMonth, setDay);
						
						if (list.size() > 0)
						{
							if (setDeleteData(list.get(0).getId()))
							{
								setCalendar();
								
								MilkAlarm ma = new MilkAlarm();
								ma.checkAlarm(superContext, superDb);
								MilkUtil.showToast(superContext, "생리일 삭제에 성공하였습니다.");
							}
							else
							{
								MilkUtil.showToast(superContext, "삭제 실패! 개발자에게 문의해주세요.");
							}
						}
						else
						{
							MilkUtil.showToast(superContext, "삭제 할 데이터가 없습니다.");
						}
						
						yesNoDialog.dismiss();
					}
				});
				
				yesNoDialog.show();
			}
		});
		
		sexDay.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pw.dismiss();
				
				// 관계일 셋팅
				int sexCount = select.getSexCountDate(setYear, setMonth, setDay);
				if (sexCount > 0)
				{
					if (setSexDelete(setYear, setMonth, setDay))
					{
						MilkUtil.showToast(superContext, "관계일 삭제에 성공 하였습니다.");
						setCalendar();
						return;
					}
				}
				else
				{
					if (setSexData())
					{
						MilkUtil.showToast(superContext, "관계일 저장에 성공 하였습니다.");
						setCalendar();
						return;
					}
				}
			}
		});
		
		memo.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pw.dismiss();
				
				Intent intent = new Intent(superContext, MemoListActivity.class);
				intent.putExtra(MemoListActivity.MEMO_YEAR, setYear);
				intent.putExtra(MemoListActivity.MEMO_MONTH, setMonth);
				intent.putExtra(MemoListActivity.MEMO_DAY, setDay);
				intent.putExtra(MemoListActivity.MEMO_LIST_FLAG, MemoListActivity.DATE);
				startActivityForResult(intent, REQUEST_CODE);
			}
		});
	}

	private void setTextList()
	{
		selectList.add(calendar1_0);
		selectList.add(calendar1_1);
		selectList.add(calendar1_2);
		selectList.add(calendar1_3);
		selectList.add(calendar1_4);
		selectList.add(calendar1_5);
		selectList.add(calendar1_6);
		
		selectList.add(calendar2_0);
		selectList.add(calendar2_1);
		selectList.add(calendar2_2);
		selectList.add(calendar2_3);
		selectList.add(calendar2_4);
		selectList.add(calendar2_5);
		selectList.add(calendar2_6);
		
		selectList.add(calendar3_0);
		selectList.add(calendar3_1);
		selectList.add(calendar3_2);
		selectList.add(calendar3_3);
		selectList.add(calendar3_4);
		selectList.add(calendar3_5);
		selectList.add(calendar3_6);
		
		selectList.add(calendar4_0);
		selectList.add(calendar4_1);
		selectList.add(calendar4_2);
		selectList.add(calendar4_3);
		selectList.add(calendar4_4);
		selectList.add(calendar4_5);
		selectList.add(calendar4_6);
		
		selectList.add(calendar5_0);
		selectList.add(calendar5_1);
		selectList.add(calendar5_2);
		selectList.add(calendar5_3);
		selectList.add(calendar5_4);
		selectList.add(calendar5_5);
		selectList.add(calendar5_6);
	
		selectList.add(calendar6_0);
		selectList.add(calendar6_1);
		selectList.add(calendar6_2);
		selectList.add(calendar6_3);
		selectList.add(calendar6_4);
		selectList.add(calendar6_5);
		selectList.add(calendar6_6);
		
		dayList.add(calendar1_0TextView);
		dayList.add(calendar1_1TextView);
		dayList.add(calendar1_2TextView);
		dayList.add(calendar1_3TextView);
		dayList.add(calendar1_4TextView);
		dayList.add(calendar1_5TextView);
		dayList.add(calendar1_6TextView);
		
		dayList.add(calendar2_0TextView);
		dayList.add(calendar2_1TextView);
		dayList.add(calendar2_2TextView);
		dayList.add(calendar2_3TextView);
		dayList.add(calendar2_4TextView);
		dayList.add(calendar2_5TextView);
		dayList.add(calendar2_6TextView);
		
		dayList.add(calendar3_0TextView);
		dayList.add(calendar3_1TextView);
		dayList.add(calendar3_2TextView);
		dayList.add(calendar3_3TextView);
		dayList.add(calendar3_4TextView);
		dayList.add(calendar3_5TextView);
		dayList.add(calendar3_6TextView);
		
		dayList.add(calendar4_0TextView);
		dayList.add(calendar4_1TextView);
		dayList.add(calendar4_2TextView);
		dayList.add(calendar4_3TextView);
		dayList.add(calendar4_4TextView);
		dayList.add(calendar4_5TextView);
		dayList.add(calendar4_6TextView);
		
		dayList.add(calendar5_0TextView);
		dayList.add(calendar5_1TextView);
		dayList.add(calendar5_2TextView);
		dayList.add(calendar5_3TextView);
		dayList.add(calendar5_4TextView);
		dayList.add(calendar5_5TextView);
		dayList.add(calendar5_6TextView);
	
		dayList.add(calendar6_0TextView);
		dayList.add(calendar6_1TextView);
		dayList.add(calendar6_2TextView);
		dayList.add(calendar6_3TextView);
		dayList.add(calendar6_4TextView);
		dayList.add(calendar6_5TextView);
		dayList.add(calendar6_6TextView);
		
		sengriList.add(sengri1_0TextView);
		sengriList.add(sengri1_1TextView);
		sengriList.add(sengri1_2TextView);
		sengriList.add(sengri1_3TextView);
		sengriList.add(sengri1_4TextView);
		sengriList.add(sengri1_5TextView);
		sengriList.add(sengri1_6TextView);
		
		sengriList.add(sengri2_0TextView);
		sengriList.add(sengri2_1TextView);
		sengriList.add(sengri2_2TextView);
		sengriList.add(sengri2_3TextView);
		sengriList.add(sengri2_4TextView);
		sengriList.add(sengri2_5TextView);
		sengriList.add(sengri2_6TextView);
		
		sengriList.add(sengri3_0TextView);
		sengriList.add(sengri3_1TextView);
		sengriList.add(sengri3_2TextView);
		sengriList.add(sengri3_3TextView);
		sengriList.add(sengri3_4TextView);
		sengriList.add(sengri3_5TextView);
		sengriList.add(sengri3_6TextView);
		
		sengriList.add(sengri4_0TextView);
		sengriList.add(sengri4_1TextView);
		sengriList.add(sengri4_2TextView);
		sengriList.add(sengri4_3TextView);
		sengriList.add(sengri4_4TextView);
		sengriList.add(sengri4_5TextView);
		sengriList.add(sengri4_6TextView);
		
		sengriList.add(sengri5_0TextView);
		sengriList.add(sengri5_1TextView);
		sengriList.add(sengri5_2TextView);
		sengriList.add(sengri5_3TextView);
		sengriList.add(sengri5_4TextView);
		sengriList.add(sengri5_5TextView);
		sengriList.add(sengri5_6TextView);
	
		sengriList.add(sengri6_0TextView);
		sengriList.add(sengri6_1TextView);
		sengriList.add(sengri6_2TextView);
		sengriList.add(sengri6_3TextView);
		sengriList.add(sengri6_4TextView);
		sengriList.add(sengri6_5TextView);
		sengriList.add(sengri6_6TextView);
		
		sexList.add(sex1_0TextView);
		sexList.add(sex1_1TextView);
		sexList.add(sex1_2TextView);
		sexList.add(sex1_3TextView);
		sexList.add(sex1_4TextView);
		sexList.add(sex1_5TextView);
		sexList.add(sex1_6TextView);
		
		sexList.add(sex2_0TextView);
		sexList.add(sex2_1TextView);
		sexList.add(sex2_2TextView);
		sexList.add(sex2_3TextView);
		sexList.add(sex2_4TextView);
		sexList.add(sex2_5TextView);
		sexList.add(sex2_6TextView);
		
		sexList.add(sex3_0TextView);
		sexList.add(sex3_1TextView);
		sexList.add(sex3_2TextView);
		sexList.add(sex3_3TextView);
		sexList.add(sex3_4TextView);
		sexList.add(sex3_5TextView);
		sexList.add(sex3_6TextView);
		
		sexList.add(sex4_0TextView);
		sexList.add(sex4_1TextView);
		sexList.add(sex4_2TextView);
		sexList.add(sex4_3TextView);
		sexList.add(sex4_4TextView);
		sexList.add(sex4_5TextView);
		sexList.add(sex4_6TextView);
		
		sexList.add(sex5_0TextView);
		sexList.add(sex5_1TextView);
		sexList.add(sex5_2TextView);
		sexList.add(sex5_3TextView);
		sexList.add(sex5_4TextView);
		sexList.add(sex5_5TextView);
		sexList.add(sex5_6TextView);
	
		sexList.add(sex6_0TextView);
		sexList.add(sex6_1TextView);
		sexList.add(sex6_2TextView);
		sexList.add(sex6_3TextView);
		sexList.add(sex6_4TextView);
		sexList.add(sex6_5TextView);
		sexList.add(sex6_6TextView);
		
		memoList.add(memo1_0TextView);
		memoList.add(memo1_1TextView);
		memoList.add(memo1_2TextView);
		memoList.add(memo1_3TextView);
		memoList.add(memo1_4TextView);
		memoList.add(memo1_5TextView);
		memoList.add(memo1_6TextView);
		
		memoList.add(memo2_0TextView);
		memoList.add(memo2_1TextView);
		memoList.add(memo2_2TextView);
		memoList.add(memo2_3TextView);
		memoList.add(memo2_4TextView);
		memoList.add(memo2_5TextView);
		memoList.add(memo2_6TextView);
		
		memoList.add(memo3_0TextView);
		memoList.add(memo3_1TextView);
		memoList.add(memo3_2TextView);
		memoList.add(memo3_3TextView);
		memoList.add(memo3_4TextView);
		memoList.add(memo3_5TextView);
		memoList.add(memo3_6TextView);
		
		memoList.add(memo4_0TextView);
		memoList.add(memo4_1TextView);
		memoList.add(memo4_2TextView);
		memoList.add(memo4_3TextView);
		memoList.add(memo4_4TextView);
		memoList.add(memo4_5TextView);
		memoList.add(memo4_6TextView);
		
		memoList.add(memo5_0TextView);
		memoList.add(memo5_1TextView);
		memoList.add(memo5_2TextView);
		memoList.add(memo5_3TextView);
		memoList.add(memo5_4TextView);
		memoList.add(memo5_5TextView);
		memoList.add(memo5_6TextView);
	
		memoList.add(memo6_0TextView);
		memoList.add(memo6_1TextView);
		memoList.add(memo6_2TextView);
		memoList.add(memo6_3TextView);
		memoList.add(memo6_4TextView);
		memoList.add(memo6_5TextView);
		memoList.add(memo6_6TextView);
	}
	
	private void setCalendar()
	{
		MilkShared ms = new MilkShared(superContext);
		
		weekdayView.setBackgroundColor(Color.parseColor(ms.getColorLine()));
		
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root, ms.getColorLine());
		
		monday.setTextColor(Color.parseColor(MilkUtil.getTextColorLine(ms.getColorLine())));
		tuesday.setTextColor(Color.parseColor(MilkUtil.getTextColorLine(ms.getColorLine())));
		wednesday.setTextColor(Color.parseColor(MilkUtil.getTextColorLine(ms.getColorLine())));
		thursday.setTextColor(Color.parseColor(MilkUtil.getTextColorLine(ms.getColorLine())));
		friday.setTextColor(Color.parseColor(MilkUtil.getTextColorLine(ms.getColorLine())));
		
		for (int i = 0; i < dayList.size(); i++)
		{
			TextView tv = (TextView) dayList.get(i);
			tv.setBackgroundColor(0);
			tv.setText("");
		}
		
		for (int i = 0; i < selectList.size(); i++)
		{
			View v = (View) selectList.get(i);
			v.setBackgroundResource(0);
			v.setOnClickListener(null);
		}
		
		for (int i = 0; i < sexList.size(); i++)
		{
			TextView tv = (TextView) sexList.get(i);
			tv.setVisibility(View.INVISIBLE);
		}
		
		for (int i = 0; i < memoList.size(); i++)
		{
			TextView tv = (TextView) memoList.get(i);
			tv.setVisibility(View.INVISIBLE);
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, setYear);
		calendar.set(Calendar.MONTH, setMonth);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		todayTextView.setText(MilkUtil.getDate(setYear, setMonth+1));

		int getDay = calendar.get(Calendar.DAY_OF_WEEK);
		
		int j = 1;

		for (int i = getDay - 1; i < calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH) + getDay - 1; i++) 
		{
			View sv = selectList.get(i);
			sv.setTag(j);
			sv.setOnClickListener(MainActivity.this);
			sv.setBackgroundResource(R.drawable.selector_normal);
			
			// 메모 셋팅
			int memoCount = select.getMemoCountDate(setYear, setMonth, j);
			if (memoCount > 0)
			{
				TextView tv = (TextView) memoList.get(i);
				tv.setVisibility(View.VISIBLE);
				tv.setBackgroundColor(Color.parseColor(ms.getColorMemo()));
				tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorMemo())));
			}
			
			// 관계일 셋팅
			int sexCount = select.getSexCountDate(setYear, setMonth, j);
			if (sexCount > 0)
			{
				TextView tv = (TextView) sexList.get(i);
				tv.setVisibility(View.VISIBLE);
				tv.setBackgroundColor(Color.parseColor(ms.getColorSex()));
				tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorSex())));
			}
			
			TextView tv = dayList.get(i);
			
			if (j == toDay) 
			{
				if ((setYear == toYear) && (setMonth == toMonth)) 
				{
					StateListDrawable states = new StateListDrawable();
					
					Drawable drawable = new ColorDrawable(Color.parseColor(ms.getColorToday()));
					states.addState(new int[] {android.R.attr.state_pressed},
							getResources().getDrawable(R.drawable.bg_hotpink));
					states.addState(new int[] {android.R.attr.state_selected},
					    getResources().getDrawable(R.drawable.bg_hotpink));
					states.addState(new int[] {-android.R.attr.state_pressed},
							drawable);
					
					sv.setBackground(states);
				}
				
				tv.setText(j + "");
			}
			else 
			{
				tv.setText(j + "");
			}
			
			j++;
		}
		
		sengriCheckList = select.getSengriList();
		
		progressDialog = new MilkProgressDialog(superContext);
		progressDialog.setCancelable(false);
		progressDialog.setMessage("잠시만 기다려주세요.");
		progressDialog.show();
		
		tharedCount = 3;
		
		for (int i = 0; i < sengriList.size(); i++)
		{
			TextView tv = (TextView) sengriList.get(i);
			tv.setBackgroundColor(0);
			tv.setText("");
			tv.setVisibility(View.INVISIBLE);
		}
		
		setSengriThread();
		setGaimThread();
		setBeranThread();
	}
	
	@Override
	public void onClick(View v) 
	{
		if (v.getId() == R.id.mainMemoView)
		{
			Intent intent = new Intent(superContext, MemoListActivity.class);
			intent.putExtra(MemoListActivity.MEMO_LIST_FLAG, MemoListActivity.ALL);
			startActivityForResult(intent, REQUEST_CODE);
		}
		else if (v.getId() == R.id.mainInfoView)
		{
			Intent intent = new Intent(superContext, InfoActivity.class);
			intent.putExtra(InfoActivity.INFO_YEAR, setYear);
			startActivityForResult(intent, REQUEST_CODE);
		}
		else if (v.getId() == R.id.mainSettingView)
		{
			Intent intent = new Intent(superContext, SettingActivity.class);
			startActivityForResult(intent, REQUEST_SETTING);
		}
		else if (v.getId() == R.id.mainTipView)
		{
			Intent intent = new Intent(superContext, TipActivity.class);
			startActivity(intent);
		}
		else if (v.getId() == R.id.calendar_picker_pre) 
		{
			if (progressDialog.isShowing())
			{
				return;
			}
			
			setMonth--;
			
			if (setMonth < 0) 
			{
				setYear--;
				setMonth = 11;
			}
			
			setCalendar();
		} 
		else if (v.getId() == R.id.calendar_picker_next) 
		{
			if (progressDialog.isShowing())
			{
				return;
			}
			
			setMonth++;
			
			if (setMonth > 11) 
			{
				setMonth = 0;
				setYear++;
			}
			
			setCalendar();
		} 
		else if(v.getId() == R.id.calendar_picker_today)
		{
			MilkMonthDialog dialog = new MilkMonthDialog(superContext, new DateCallBank()
			{
				@Override
				public void getDate(int year, int month)
				{
					setYear = year;
					setMonth = month;
					setCalendar();
				}
			}, setYear);
			
			dialog.show();
		}
		else 
		{
			if (v.getTag() != null && (Integer) v.getTag() > 0)
			{
				setDay = (int) v.getTag();
				ArrayList<SengriData> list = select.getSengriDate(setYear, setMonth, setDay);
				
				Calendar cal = Calendar.getInstance();
				cal.set(setYear, setMonth, setDay);
				cal.add(Calendar.DAY_OF_MONTH, -9);
				
				int year = cal.get(Calendar.YEAR);
				int month = cal.get(Calendar.MONTH);
				int day = cal.get(Calendar.DAY_OF_MONTH);
				
				ArrayList<SengriData> list2 = select.getSengriDate2(year, month, day, setYear, setMonth, setDay);
				selectSengriData = null;
				
				if (list.size() > 0)
				{
					startView.setVisibility(View.GONE);
					endView.setVisibility(View.GONE);
					termView.setVisibility(View.VISIBLE);
					deleteView.setVisibility(View.VISIBLE);
				}
				else if (list2.size() > 0)
				{
					selectSengriData = list2.get(0);
					
					startView.setVisibility(View.VISIBLE);
					endView.setVisibility(View.VISIBLE);
					termView.setVisibility(View.GONE);
					deleteView.setVisibility(View.GONE);
				}
				else
				{
					startView.setVisibility(View.VISIBLE);
					endView.setVisibility(View.GONE);
					termView.setVisibility(View.GONE);
					deleteView.setVisibility(View.GONE);
				}
				
				pw.showAsDropDown(v);
			}
		}
	}
	
	private boolean setSexData()
	{
		DbInsertQuery query = null;
		
		try
		{
			query = new DbInsertQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("year", setYear + "");
			map.put("month", setMonth + "");
			map.put("day", setDay + "");
			
			query.setSexDate(map);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	private boolean setSexDelete(int year, int month, int day)
	{
		DbDeleteQuery query = null;
		
		try
		{
			query = new DbDeleteQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			query.setSexDelete(year, month, day);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	private boolean setPeriodData(int year, int month, int day)
	{
		DbInsertQuery query = null;
		
		try
		{
			query = new DbInsertQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			MilkShared s = new MilkShared(superContext);
			
			map.put("year", year + "");
			map.put("month", month + "");
			map.put("day", day + "");
			map.put("term", s.getSengriDate() + "");
			map.put("date", MilkUtil.getDateDB(year, month, day));
			
			query.setPeriodDate(map);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	private boolean setPeriodUpdate(int year, int month, int day, int id, int term)
	{
		DbUpdateQuery query = null;
		
		try
		{
			query = new DbUpdateQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			if (term > 0)
			{
				map.put("term", term + "");
			}
			
			map.put("year", year + "");
			map.put("month", month + "");
			map.put("day", day + "");
			map.put("date", MilkUtil.getDateDB(year, month, day));
			
			query.setPeriodUpdate(id, map);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}

	private boolean setDeleteData(int id)
	{
		DbDeleteQuery query = null;
		
		try
		{
			query = new DbDeleteQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			query.setPeriodDelete(id);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_CODE)
		{
			setCalendar();
		}
		else if (requestCode == REQUEST_SETTING)
		{
			int flag = data.getIntExtra(SettingActivity.RESET, 0);
			
			if (flag == 0)
			{
				setCalendar();
			}
			else
			{
				Intent intent = new Intent(MainActivity.this, SengriActivity.class);
				intent.putExtra(SengriActivity.SENGRI_FLAG, SengriActivity.FIRST);
				startActivity(intent);
				finish();
			}
		}
	}
	
	/**
	 * flag 0 = 생리일, 1 = 가임일, 2 = 배란일
	 * @param year
	 * @param month
	 * @param day
	 * @param flag
	 * @return
	 */
	private int getSengriList(int year, int month, int day, int flag)
	{
		int count = 0;
		
		MilkShared s = new MilkShared(superContext);
		
		int term = s.getSengriTerm();
		
		if (s.getSengriType() == 0)
		{
			term = s.getSengriTerm();
		}
		else
		{
			term = MilkUtil.getAveSengriTerm(superContext, select);
		}
		
		if (flag == 0)
		{
			for (int i = 0; i < 10; i++)
			{
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(0);
				cal.set(year, month, day);
				cal.add(Calendar.DAY_OF_MONTH, -i);
				
				count = getSengriCheck(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
				
				if (count > 0)
				{
					ArrayList<SengriData> list = 
							select.getSengriDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
					
					if (i == 0)
					{
						return 4;
					}
					else if (list.get(0).getTerm() < i+1)
					{
						return 0;
					}
					else
					{
						return 3;
					}
				}
			}
			
			int sengriDate = s.getSengriDate();
			
			SengriData data = select.getSengriMaxDate();
			Calendar cal2 = Calendar.getInstance();
			cal2.setTimeInMillis(0);
			cal2.set(data.getYear(), data.getMonth(), data.getDay());
			
			long mill2 = cal2.getTimeInMillis();
			
			for (int i = 0; i < sengriDate; i++)
			{
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(0);
				cal.set(year, month, day);
				cal.add(Calendar.DAY_OF_MONTH, -i);
				long mill1 = cal.getTimeInMillis();
				
				mill1 = mill1 - mill2;
				
				long mog = mill1 / (24 * 60 * 60 * 1000);
				
				long sengriTerm = mog / term;
				long namugi = mog % term;
				
				if (sengriTerm > 0 && namugi == 0)
				{
					Calendar cal3 = Calendar.getInstance();
					cal3.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
					cal3.add(Calendar.DAY_OF_MONTH, - (int) mog);
					
					count = getSengriCheck(cal3.get(Calendar.YEAR), cal3.get(Calendar.MONTH), cal3.get(Calendar.DAY_OF_MONTH));
					
					if (count > 0)
					{
						if (i == 0)
						{
							return 2;
						}
						else
						{
							return 1;
						}
					}
				}
			}
		}
		else if (flag == 1)
		{
			int gaimDate = 6;
			
			for (int i = 0; i < gaimDate; i++)
			{
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(0);
				cal.set(year, month, day);
				cal.add(Calendar.DAY_OF_MONTH, 17);
				cal.add(Calendar.DAY_OF_MONTH, -i);
				
				count = getSengriCheck(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
				
				if (count > 0)
				{
					if (i == 3)
					{
						return 0;
					}
					
					if (i == 0)
					{
						return 4;
					}
					else
					{
						return 3;
					}
				}
			}
			
			SengriData data = select.getSengriMaxDate();
			Calendar cal2 = Calendar.getInstance();
			cal2.setTimeInMillis(0);
			cal2.set(data.getYear(), data.getMonth(), data.getDay());
			
			long mill2 = cal2.getTimeInMillis();
			
			for (int i = 0; i < gaimDate; i++)
			{
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(0);
				cal.set(year, month, day);
				cal.add(Calendar.DAY_OF_MONTH, 17);
				cal.add(Calendar.DAY_OF_MONTH, -i);
				long mill1 = cal.getTimeInMillis();
				
				mill1 = mill1 - mill2;
				
				long mog = mill1 / (24 * 60 * 60 * 1000);
				
				long sengriTerm = mog / term;
				long namugi = mog % term;
				
				if (sengriTerm > 0 && namugi == 0)
				{
					Calendar cal3 = Calendar.getInstance();
					cal3.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
					cal3.add(Calendar.DAY_OF_MONTH, - (int) mog);
					
					count = getSengriCheck(cal3.get(Calendar.YEAR), cal3.get(Calendar.MONTH), cal3.get(Calendar.DAY_OF_MONTH));
					
					if (count > 0)
					{
						if (i == 3)
						{
							return 0;
						}
						
						if (i == 0)
						{
							return 2;
						}
						else
						{
							return 1;
						}
					}
				}
			}
		}
		else if (flag == 2)
		{
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(0);
			cal.set(year, month, day);
			cal.add(Calendar.DAY_OF_MONTH, 14);
			
			count = getSengriCheck(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
			
			if (count > 0)
			{
				return 2;
			}
			
			SengriData data = select.getSengriMaxDate();
			Calendar cal2 = Calendar.getInstance();
			cal2.setTimeInMillis(0);
			cal2.set(data.getYear(), data.getMonth(), data.getDay());
			
			long mill2 = cal2.getTimeInMillis();
			
			Calendar calt = Calendar.getInstance();
			calt.setTimeInMillis(0);
			calt.set(year, month, day);
			calt.add(Calendar.DAY_OF_MONTH, 14);
			long mill1 = calt.getTimeInMillis();
			
			mill1 = mill1 - mill2;
			
			long mog = mill1 / (24 * 60 * 60 * 1000);
			
			long sengriTerm = mog / term;
			long namugi = mog % term;
			
			if (sengriTerm > 0 && namugi == 0)
			{
				Calendar cal3 = Calendar.getInstance();
				cal3.set(calt.get(Calendar.YEAR), calt.get(Calendar.MONTH), calt.get(Calendar.DAY_OF_MONTH));
				cal3.add(Calendar.DAY_OF_MONTH, - (int) mog);
				
				count = getSengriCheck(cal3.get(Calendar.YEAR), cal3.get(Calendar.MONTH), cal3.get(Calendar.DAY_OF_MONTH));
				
				if (count > 0)
				{
					return 1;
				}
			}
		}
		
		return 0;
	}
	
	private int getSengriCheck(int year, int month, int day)
	{
		for (int i = 0; i < sengriCheckList.size(); i++)
		{
			SengriData data = sengriCheckList.get(i);
			
			if (data.getYear() == year && data.getMonth() == month && data.getDay() == day)
			{
				return 1;
			}
		}
		
		return 0;
	}
	
	private void setSengriThread()
	{
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				if (sengriCheckList.size() < 1)
				{
					sengriHandler.sendMessage(sengriHandler.obtainMessage());
				}
				
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.YEAR, setYear);
				calendar.set(Calendar.MONTH, setMonth);
				
				for (int i = 1; i < calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1; i++) 
				{
					SengGaBeData data = sgbList.get(i);
					
					// 생리일 셋팅
					int sengriCount = getSengriList(setYear, setMonth, i, 0);
					if (sengriCount == 1)
					{
						data.setSengri(true);
						data.setSengriStart(false);
						data.setSengriReal(false);
					}
					else if (sengriCount == 2)
					{
						data.setSengri(true);
						data.setSengriStart(true);
						data.setSengriReal(false);
					}
					else if (sengriCount == 3)
					{
						data.setSengri(true);
						data.setSengriStart(false);
						data.setSengriReal(true);
					}
					else if (sengriCount == 4)
					{
						data.setSengri(true);
						data.setSengriStart(true);
						data.setSengriReal(true);
					}
					else
					{
						data.setSengri(false);
						data.setSengriStart(false);
						data.setSengriReal(false);
					}
				}
				
				sengriHandler.sendMessage(sengriHandler.obtainMessage());
			}
		}).start();
	}
	
	private void setGaimThread()
	{
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				if (sengriCheckList.size() < 1)
				{
					gaimHandler.sendMessage(gaimHandler.obtainMessage());
				}
				
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.YEAR, setYear);
				calendar.set(Calendar.MONTH, setMonth);
				
				for (int i = 1; i < calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1; i++) 
				{
					SengGaBeData data = sgbList.get(i);
					
					// 가임일 셋팅
					int gaimCount = getSengriList(setYear, setMonth, i, 1);
					if (gaimCount == 1)
					{
						data.setGaim(true);
						data.setGaimStart(false);
						data.setGaimReal(false);
					}
					else if (gaimCount == 2)
					{
						data.setGaim(true);
						data.setGaimStart(true);
						data.setGaimReal(false);
					}
					else if (gaimCount == 3)
					{
						data.setGaim(true);
						data.setGaimStart(false);
						data.setGaimReal(true);
					}
					else if (gaimCount == 4)
					{
						data.setGaim(true);
						data.setGaimStart(true);
						data.setGaimReal(true);
					}
					else
					{
						data.setGaim(false);
						data.setGaimStart(false);
						data.setGaimReal(false);
					}
				}
				
				gaimHandler.sendMessage(gaimHandler.obtainMessage());
			}
		}).start();
	}
	
	private void setBeranThread()
	{
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				if (sengriCheckList.size() < 1)
				{
					beranHandler.sendMessage(beranHandler.obtainMessage());
				}
				
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.YEAR, setYear);
				calendar.set(Calendar.MONTH, setMonth);
				
				for (int i = 1; i < calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1; i++) 
				{
					SengGaBeData data = sgbList.get(i);
					
					// 배란일 셋팅
					int beranCount = getSengriList(setYear, setMonth, i, 2);
					if (beranCount == 1)
					{
						data.setBeran(true);
						data.setBeranReal(false);
					}
					else if (beranCount == 2)
					{
						data.setBeran(true);
						data.setBeranReal(true);
					}
					else
					{
						data.setBeran(false);
					}
				}
				
				beranHandler.sendMessage(beranHandler.obtainMessage());
			}
		}).start();
	}
	
	private Handler sengriHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			MilkShared ms = new MilkShared(superContext);
			
			tharedCount = tharedCount -1;
			
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, setYear);
			calendar.set(Calendar.MONTH, setMonth);
			calendar.set(Calendar.DAY_OF_MONTH, 1);

			int getDay = calendar.get(Calendar.DAY_OF_WEEK);
			
			int j = 1;

			for (int i = getDay - 1; i < calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH) + getDay - 1; i++) 
			{
				SengGaBeData data = sgbList.get(j);
				
				// 생리일 셋팅
				boolean sengri = data.isSengri();
				if (sengri)
				{
					TextView tv = (TextView) sengriList.get(i);
					tv.setVisibility(View.VISIBLE);
					
					if (data.isSengriReal())
					{
//						tv.setBackgroundResource(R.drawable.bg_sengri);
						
						tv.setBackgroundColor(Color.parseColor(ms.getColorSengri()));
						
						if (data.isSengriStart())
						{
							tv.setText("생리일");
							tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorSengri())));
						}
					}
					else
					{
//						tv.setBackgroundResource(R.drawable.bg_sengri_due);
						
						String color = ms.getColorSengri();
						color = color.replace("#", "#bb");
						tv.setBackgroundColor(Color.parseColor(color));
						
						if (data.isSengriStart())
						{
							tv.setText("생리예정");
							tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorSengri())));
						}
					}
				}
				
				j++;
			}
			
			if (tharedCount == 0)
			{
				progressDialog.dismiss();
			}
		}
		
	};
	
	private Handler gaimHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			MilkShared ms = new MilkShared(superContext);
			
			tharedCount = tharedCount -1;
			
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, setYear);
			calendar.set(Calendar.MONTH, setMonth);
			calendar.set(Calendar.DAY_OF_MONTH, 1);

			int getDay = calendar.get(Calendar.DAY_OF_WEEK);
			
			int j = 1;

			for (int i = getDay - 1; i < calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH) + getDay - 1; i++) 
			{
				SengGaBeData data = sgbList.get(j);
				
				// 가임일 셋팅
				boolean gaim = data.isGaim();
				if (gaim)
				{
					TextView tv = (TextView) sengriList.get(i);
					tv.setVisibility(View.VISIBLE);
					
					if (data.isGaimReal())
					{
//						tv.setBackgroundResource(R.drawable.bg_gaim);
						
						tv.setBackgroundColor(Color.parseColor(ms.getColorGaim()));
						
						if (data.isGaimStart())
						{
							tv.setText("가임일");
							tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorGaim())));
						}
					}
					else
					{
//						tv.setBackgroundResource(R.drawable.bg_gaim_due);
						
						String color = ms.getColorGaim();
						color = color.replace("#", "#bb");
						tv.setBackgroundColor(Color.parseColor(color));
						
						if (data.isGaimStart())
						{
							tv.setText("가임예정");
							tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorGaim())));
						}
					}
				}
				
				j++;
			}
			
			if (tharedCount == 0)
			{
				progressDialog.dismiss();
			}
		}
	};
	
	private Handler beranHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			MilkShared ms = new MilkShared(superContext);
			
			tharedCount = tharedCount -1;
			
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, setYear);
			calendar.set(Calendar.MONTH, setMonth);
			calendar.set(Calendar.DAY_OF_MONTH, 1);

			int getDay = calendar.get(Calendar.DAY_OF_WEEK);
			
			int j = 1;

			for (int i = getDay - 1; i < calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH) + getDay - 1; i++) 
			{
				SengGaBeData data = sgbList.get(j);
				
				// 배란일 셋팅
				boolean beran = data.isBeran();
				if (beran)
				{
					TextView tv = (TextView) sengriList.get(i);
					tv.setVisibility(View.VISIBLE);
					
					if (data.isBeranReal())
					{
						tv.setText("배란일");
//						tv.setBackgroundResource(R.drawable.bg_beran);
						
						tv.setBackgroundColor(Color.parseColor(ms.getColorBeran()));
						tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorBeran())));
					}
					else 
					{
						tv.setText("배란예정");
//						tv.setBackgroundResource(R.drawable.bg_beran_due);
						
						String color = ms.getColorBeran();
						color = color.replace("#", "#bb");
						tv.setBackgroundColor(Color.parseColor(color));
						tv.setTextColor(Color.parseColor(MilkUtil.getTextColorSelect(ms.getColorBeran())));
					}
				}
				
				j++;
			}
			
			if (tharedCount == 0)
			{
				progressDialog.dismiss();
			}
		}
	};
	
	private Handler backHandler = new Handler()
    {
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			if (msg.what == 0)
			{
				backFlag = false;
			}
		}
    };
	
	@Override
	public void onBackPressed()
	{
		if (pw.isShowing())
		{
			pw.dismiss();
			return;
		}
		
		if (!backFlag)
		{
			MilkUtil.showToast(superContext, "뒤로 버튼을 한번더 누르시면 종료됩니다.");
			backFlag = true;
			
			backHandler.sendEmptyMessageDelayed(0, 2000);
		}
		else
		{
			superDb = DbAdapter.getInstance();
    		superDb.close();
			super.onBackPressed();
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		if (progressDialog.isShowing())
		{
			return false;
		}
		
		float distance = 0;
		
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			touchX = event.getX();
		}
		else if (event.getAction() == MotionEvent.ACTION_UP)
		{
			distance = touchX - event.getX();
		}
		
		// 해당 거리가 100이 되지 않으면 이벤트 처리 하지 않는다.
		if (Math.abs(distance) < 100) 
		{
			return false;	
		}
						
		// 손가락을 왼쪽으로 움직였으면 오른쪽 화면이 나타나야 한다.
		if (distance > 0) 
		{
			setMonth++;
			
			if (setMonth > 11) 
			{
				setMonth = 0;
				setYear++;
			}
			
			setCalendar();
		} 
		// 손가락을 오른쪽으로 움직였으면 왼쪽 화면이 나타나야 한다.
		else 
		{
			setMonth--;
			
			if (setMonth < 0) 
			{
				setYear--;
				setMonth = 11;
			}
			
			setCalendar();
		}
		
		return true;
	}
	
	private void setGlobalFont(ViewGroup root, String color) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof ImageView)
	        {
	        	((ImageView)child).setBackgroundColor(Color.parseColor(color));
	        }
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child, color);
	        }
	    }
	}
}
